﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"game.h"

void InitBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
			else
				printf("\n");
		}
		for (int j = 0; j < col; j++)
		{
			if (i < row - 1)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
				else
					printf("\n");
			}
		}
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int a = 1;
	do
	{
		printf("玩家走:>\n");
		printf("请输入坐标:>\n");
		int x = 0, y = 0;
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				a = 0;
			}
			else
			{
				printf("该坐标被占用\n");
			}
		}
		else
		{
			printf("坐标非法，请重新输入\n");
		}
	} while (a);
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑走:>\n");
	int a = 1;
	do
	{
		int x = 0, y = 0;
		srand((unsigned)time(NULL));
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '1';
			a = 0;
		}
	} while (a);
}

int Judge(char board[ROW][COL], int row, int col, int win)
{
	for (int x = 0; x < row; x++)//判断行
	{
		for (int y = 0; y < col - win + 1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x][y + k] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int y = 0; y < col; y++)//判断列
	{
		for (int x = 0; x < row - win + 1; x++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x + k][y] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int x = 0; x < row - win + 1; x++)//判断右斜
	{
		for (int y = 0; y < col - win + 1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x + k][y + k] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int x = win; x < row + 1; x++)//判断左斜
	{
		for (int y = 0; y < col - win + 1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x - k][y + k] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	int count = 0;
	for (int x = 0; x < row; x++)
	{
		for (int y = 0; y < col; y++)
			if (board[x][y] != ' ')
				count++;
	}
	if (count == row * col)
		return 3;
	return 4;
}